package com.play.parse.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.play.parse.JsoupHandler;

import edu.uci.ics.crawler4j.parser.ExtractedUrlAnchorPair;

public class YihaoHandler extends JsoupHandler {
	protected static final Logger logger = Logger.getLogger(YihaoHandler.class
			.getName());

	protected void productSeed(Document doc) {
		Elements type = doc.select(".alonesort"); 
		if(type.size()>0){
				Elements sees = type.select("dt>a");
				for (Element element : sees) {
					ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
					String seedurl = element.attr("href");
					curUrl.setHref(seedurl);
					//logger.info(seedurl);
					outgoingurls.add(curUrl);
				}		
		}
	}

	protected void productList(Document doc) {
		// 获取列表
		Elements e = doc.select(".itemSearchList");
		if (e.size() > 0) {
			Elements name = e.select(".title,.price");
			List<String> prodects = new ArrayList<String>();
			int x = 1;
			// 获取产品名称和价格
			for (Element p : name) {
				if (x % 2 == 1) {
					prodects.add("<product>");
				}
				if (p.tagName().equals("a")) {
					String attr = p.attr("href");
					prodects.add(p.text());
					prodects.add(attr);
					ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
					if (StringUtils.isBlank(attr)) {
						continue;
					}
					curUrl.setHref(attr);
					outgoingurls.add(curUrl);
				} else {
					prodects.add(p.select("strong").text());
				}
				if (x % 2 == 0) {
					prodects.add("</product>");
				}
				x++;
			}

			try {
				FileUtils.writeLines(new File(filePath + File.separator
						+ "fankeproductlist.txt"), prodects, true);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// 获取分页信息
			Elements pages = doc.select(".turnPageBottom").select("a");
			for (Element page : pages) {
				String pageUrl = page.attr("href");
				// logger.info(pageUrl);
				ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
				curUrl.setHref(pageUrl);
				outgoingurls.add(curUrl);
			}
		}
	}

	protected void productInfo(String contextURL, Document doc) {
		/*
		 * if(contextURL.contains("item")){ logger.info(contextURL);
		 * logger.info(doc); }
		 */
		// 两套详细页面
		Elements info = doc.select(".specific_info2");
		// logger.info(info);
		if (info.size() > 0) {
			logger.info(doc); 
			List<String> prodect = new ArrayList<String>();
			prodect.add("<product>");
			prodect.add(contextURL);
			// 获取位置（类别路径）信息
			Elements type = doc.select(".detailnav");
			prodect.add(type.text());

			// 产品名称
			Elements name = doc.select(".p_title");
			prodect.add(name.text());
			// 获取产品信息
			Elements atrr = doc.select(".detail_wrapper");
			prodect.add(atrr.text());

			// TODO 获取价格信息 拿不到
			// logger.info(info.select("#SpecialPrice").text());
			prodect.add("</product>");
			try {
				FileUtils.writeLines(new File(filePath + File.separator
						+ "fankeproduct.txt"), prodect, true);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}

	@Override
	protected String getFilePath() {
		return "F:\\machine_study\\中文语料\\电商分类\\mybest";
	}

}
